﻿// Jinx AJAX Framework
// Copyright (C) 2008 Blake Hegerle
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using Formality.Levels;
using Formality.FormalSystem;
using Formality.Game;

namespace Formality.Game
{
    using Formality.FormalSystem.Parser;
    using Formality.FormalSystem.Reasoning;
    using Core;

    public class Engine<T>
        where T : class
    {
        public readonly Predicate<T> IsGoal;
        private readonly Func<string, T> Parser;
        private readonly Func<T, Tuple<T, bool>[], bool> Reasoner;
        private readonly IEnumerable<Sentence<T>> Sentences;

        public Engine(Func<string, T> Parser, Func<T, Tuple<T, bool>[], bool> Reasoner, Predicate<T> IsGoal)
        {
            this.Parser = Parser;
            this.Reasoner = Reasoner;
            this.IsGoal = IsGoal;

            Sentences = new FullEnumerator<T>(Parser);
        }

        public Sentence<T> this[string Id]
        {
            get
            {
                return (from S in Sentences
                        where S.Id == Id
                        select S).SingleOrDefault();
            }
        }

        public T Parse(string Input)
        {
            return Parser(Input);
        }

        public IEnumerable<Sentence<T>> GetPremises(string Id)
        {
            return Sentences.TakeWhile(S => S.Id != Id);
        }

        public string Next(string Id)
        {
            var Iterator = Sentences.GetEnumerator();

            while (Iterator.MoveNext())
                if (Iterator.Current.Id == Id)
                {
                    while (Iterator.MoveNext())
                    {
                        if (IsGoal(Iterator.Current.Item))
                            return Iterator.Current.Id;
                    }

                    return null;
                }

            throw new Exception("Could not find sentence with Id " + Id);
        }

        public Inference? Follows(T Proposal, IEnumerable<Tuple<T, bool>> Premises)
        {
            if (Reasoner(Proposal, Premises.ToArray()))
                return new Inference();

            return null;
        }
    }

    public class Engine : Engine<Proposition>
    {
        public Engine()
            : base(i => i.Parse(), (g, p) => g.Follows(p) != null, p => p.IsGoal)
        {
        }
    }
}